package com.tkjt.pms.calc.formula.java.common;

import java.math.BigDecimal;

public class FaDoubleUtil {

    private static final int DEF_DIV_SCALE = 8;

    /**
     * * 两个Double数相加 *
     *
     * @param v1 *
     * @param v2 *
     * @return Double
     */
    public static Double add(Double v1, Double v2) {
        BigDecimal b1 = new BigDecimal(v1.toString());
        BigDecimal b2 = new BigDecimal(v2.toString());
        return b1.add(b2).doubleValue();
    }
    public static Double add(Double v1, Double v2, Double v3) {
        return add(add(v1,v2),v3);
    }

    /**
     * * 两个Double数相减 *
     *
     * @param v1 *
     * @param v2 *
     * @return Double
     */
    public static Double sub(Double v1, Double v2) {
        BigDecimal b1 = new BigDecimal(v1.toString());
        BigDecimal b2 = new BigDecimal(v2.toString());
        return b1.subtract(b2).doubleValue();
    }

    /**
     * * 两个Double数相乘 *
     *
     * @param v1 *
     * @param v2 *
     * @return Double
     */
    public static Double mul(Double v1, Double v2) {
        BigDecimal b1 = new BigDecimal(v1.toString());
        BigDecimal b2 = new BigDecimal(v2.toString());
        return b1.multiply(b2).doubleValue();
    }

    /**
     * * 两个Double数相除 *
     *
     * @param v1 *
     * @param v2 *
     * @return Double
     * 默认保留两位小数
     */
    public static Double div(Double v1, Double v2) {
        if (Double.isNaN(v2) || Double.isInfinite(v2)|| Double.isInfinite(v1)) {
            return 0.00;
        }
        if (v2==0) {
            return 0.00;
        }
        BigDecimal b1 = new BigDecimal(v1.toString());
        BigDecimal b2 = new BigDecimal(v2.toString());
        return b1.divide(b2, DEF_DIV_SCALE, BigDecimal.ROUND_HALF_UP).doubleValue();
    }

    /**
     * * 两个Double数相除，并保留scale位小数 *
     *
     * @param v1 *
     * @param v2 *
     * @param scale *
     * @return Double
     */
    public static Double div(Double v1, Double v2, int scale) {
        if (Double.isNaN(v2) || Double.isInfinite(v2)|| Double.isInfinite(v1)) {
            return 0.00;
        }
        if (v2==0) {
            return 0.00;
        }
        if (scale < 0) {
            throw new IllegalArgumentException("The scale must be a positive integer or zero");
        }
        BigDecimal b1 = new BigDecimal(v1.toString());
        BigDecimal b2 = new BigDecimal(v2.toString());
        return b1.divide(b2, 8, BigDecimal.ROUND_HALF_UP).doubleValue();
    }

    public static Double getDouble(Double v1, int scale) {
        return  new BigDecimal(v1).setScale(8, BigDecimal.ROUND_HALF_UP).doubleValue();
    }

    public static Double getDouble(Double v1) {
        return  new BigDecimal(v1).setScale(8, BigDecimal.ROUND_HALF_UP).doubleValue();
    }

    /**
     * 初始化公式入参数据
     * @param v1
     * @return
     */
    public static Double toD(Double v1) {
        if (v1 == null){
            return 0.0;
        }
        return  v1;
    }

}
